//package com.exrobot.module.rbt.service.salefood;
//
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.date.DateTime;
//import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
//import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
//import com.exrobot.module.rbt.controller.app.vo.MealCodeVO;
//import com.exrobot.module.rbt.enums.RbtSaleFoodStatusEnum;
//import com.exrobot.module.rbt.service.mqtt.MqttMessageService;
//import com.exrobot.module.rbt.service.robotScheduler.RobotFood;
//import com.exrobot.module.rbt.service.robotScheduler.SaleOrder;
//import com.exrobot.module.rbt.service.robotScheduler.SaleOrderItem;
//import com.exrobot.module.rbt.service.robotinfo.RobotInfoService;
//import org.springframework.context.annotation.Lazy;
//import org.springframework.stereotype.Service;
//import jakarta.annotation.Resource;
//import org.springframework.validation.annotation.Validated;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.time.LocalDate;
//import java.time.LocalDateTime;
//import java.time.LocalTime;
//import java.util.*;
//import java.util.stream.Collectors;
//
//import com.exrobot.module.rbt.controller.admin.salefood.vo.*;
//import com.exrobot.module.rbt.dal.dataobject.salefood.SaleFoodDO;
//import com.exrobot.framework.common.pojo.PageResult;
//import com.exrobot.framework.common.pojo.PageParam;
//import com.exrobot.framework.common.util.object.BeanUtils;
//
//import com.exrobot.module.rbt.dal.mysql.salefood.SaleFoodMapper;
//
//import static com.exrobot.framework.common.exception.util.ServiceExceptionUtil.exception;
//import static com.exrobot.framework.common.util.collection.CollectionUtils.convertList;
//import static com.exrobot.framework.common.util.collection.CollectionUtils.diffList;
//import static com.exrobot.module.rbt.enums.ErrorCodeConstants.*;
//
///**
// * 可销售食品 Service 实现类
// *
// * @author 超级管理员
// */
//@Service
//@Validated
//public class SaleFoodServiceImpl implements SaleFoodService {
//
//    @Resource
//    private SaleFoodMapper saleFoodMapper;
//
//    @Resource
//    private RobotInfoService robotInfoService;
//
//    @Lazy
//    @Resource
//    private MqttMessageService mqttMessageService;
//
//    @Override
//    public Long createSaleFood(SaleFoodSaveReqVO createReqVO) {
//        // 插入
//        SaleFoodDO saleFood = BeanUtils.toBean(createReqVO, SaleFoodDO.class);
//        saleFoodMapper.insert(saleFood);
//
//        // 返回
//        return saleFood.getId();
//    }
//
//    @Override
//    public Long createBatchSaleFood(List<SaleFoodDO> list) {
//        // 插入
//        saleFoodMapper.insertBatch(list);
//        return 1L;
//    }
//
//    @Override
//    public void updateSaleFood(SaleFoodSaveReqVO updateReqVO) {
//        // 校验存在
//        validateSaleFoodExists(updateReqVO.getId());
//        // 更新
//        SaleFoodDO updateObj = BeanUtils.toBean(updateReqVO, SaleFoodDO.class);
//        saleFoodMapper.updateById(updateObj);
//    }
//
//    @Override
//    public void updateSaleFoodCompleted(String sn,String code) {
//        // 更新
//        LocalDate today = LocalDate.now();
//        LocalDateTime startTime = LocalDateTime.of(today, LocalTime.MIN);
//        LocalDateTime endTime = LocalDateTime.of(today, LocalTime.MAX);
//        LambdaUpdateWrapper<SaleFoodDO> updateWrapper = new LambdaUpdateWrapper<SaleFoodDO>();
//        updateWrapper.eq(SaleFoodDO::getRobotSn, sn)
//                .eq(SaleFoodDO::getCode, code)
//                .between(SaleFoodDO::getMealDate, startTime, endTime)
//                .eq(SaleFoodDO::getStatus, RbtSaleFoodStatusEnum.ORDER.getVal())
//                .set(SaleFoodDO::getStatus, RbtSaleFoodStatusEnum.COMPLETED.getVal());
//        saleFoodMapper.update(null, updateWrapper);
//    }
//
//    @Override
//    public void updateSaleFoodChange(String sn,List<String> codes) {
//        LocalDate today = LocalDate.now();
//        LocalDateTime startTime = LocalDateTime.of(today, LocalTime.MIN);
//        LocalDateTime endTime = LocalDateTime.of(today, LocalTime.MAX);
//        LambdaUpdateWrapper<SaleFoodDO> updateWrapper = new LambdaUpdateWrapper<SaleFoodDO>();
//        if(codes != null && !codes.isEmpty()) {
//            updateWrapper.in(SaleFoodDO::getCode, codes);
//        }
//        updateWrapper.eq(SaleFoodDO::getRobotSn, sn)
//                .between(SaleFoodDO::getMealDate, startTime, endTime)
//                .eq(SaleFoodDO::getStatus, RbtSaleFoodStatusEnum.ORDER.getVal())
//                .set(SaleFoodDO::getOrderId,"")
//                .set(SaleFoodDO::getPoint,"")
//                .set(SaleFoodDO::getCode,"")
//                .set(SaleFoodDO::getStatus, RbtSaleFoodStatusEnum.SALE.getVal());
//        saleFoodMapper.update(null, updateWrapper);
//    }
//
//    @Override
//    public void deleteSaleFood(Long id) {
//        // 校验存在
//        validateSaleFoodExists(id);
//        // 删除
//        saleFoodMapper.deleteById(id);
//    }
//
//    @Override
//        public void deleteSaleFoodListByIds(List<Long> ids) {
//        // 删除
//        saleFoodMapper.deleteByIds(ids);
//        }
//
//
//    private void validateSaleFoodExists(Long id) {
//        if (saleFoodMapper.selectById(id) == null) {
//            throw exception(RBT_DATA_NOT_EXISTS);
//        }
//    }
//
//    @Override
//    public SaleFoodDO getSaleFood(Long id) {
//        return saleFoodMapper.selectById(id);
//    }
//
//    @Override
//    public PageResult<SaleFoodDO> getSaleFoodPage(SaleFoodPageReqVO pageReqVO) {
//        return saleFoodMapper.selectPage(pageReqVO);
//    }
//
//    @Override
//    public List<SaleFoodDO> getSaleFoodList(SaleFoodPageReqVO pageReqVO) {
//        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
//        return saleFoodMapper.selectPage(pageReqVO).getList();
//    }
//
//    @Override
//    public List<RobotFood> getRobotSalseFoodByToday() {
//        List<RobotFood> robotFoodList = new ArrayList<>();
//        List<SaleFoodDO> list = getSalseFoodByToday(null);
//        if(list == null || list.isEmpty()) {
//            return robotFoodList;
//        }
//        Map<String,List<SaleFoodDO>> getRobotMap = list.stream().collect(Collectors.groupingBy(SaleFoodDO::getRobotSn));
//        for (Map.Entry<String, List<SaleFoodDO>> item : getRobotMap.entrySet()) {
//            String sn = item.getKey();
//            int floor = robotInfoService.getRobotFloor(sn);
//            List<SaleFoodDO> saleFoodDOList = item.getValue();
//            Map<Long, Integer> foodMap = saleFoodDOList.stream().collect(Collectors.groupingBy(
//                    SaleFoodDO::getMenuSn, // 分组键：商品ID
//                    Collectors.summingInt(SaleFoodDO::getNum)));
//            robotFoodList.add(new RobotFood(sn, floor, foodMap));
//        }
//        return robotFoodList;
//
//    }
//
//    @Override
//    public List<SaleFoodDO> assignOrderToRobot(SaleOrder order, List<String> sns) {
//        LocalDate today = LocalDate.now();
//        LocalDateTime startTime = LocalDateTime.of(today, LocalTime.MIN);
//        LocalDateTime endTime = LocalDateTime.of(today, LocalTime.MAX);
//        for(SaleOrderItem item : order.getItems()) {
//            LambdaUpdateWrapper<SaleFoodDO> updateWrapper = new LambdaUpdateWrapper<>();
//            updateWrapper.in(SaleFoodDO::getRobotSn,sns)
//                    .between(SaleFoodDO::getMealDate,startTime,endTime)
//                    .eq(SaleFoodDO::getStatus,RbtSaleFoodStatusEnum.SALE.getVal())
//                    .eq(SaleFoodDO::getMenuSn,item.getFoodId())
//                    .set(SaleFoodDO::getOrderId,order.getId())
//                    .set(SaleFoodDO::getCode,order.getCode())
//                    .set(SaleFoodDO::getPoint,order.getPoint())
//                    .set(SaleFoodDO::getStatus,RbtSaleFoodStatusEnum.ORDER.getVal())
//                    .last("ORDER BY robotBoxSn ASC LIMIT "+item.getNum());
//            saleFoodMapper.update(null,updateWrapper);
//        }
//        List<SaleFoodDO> list = saleFoodMapper.selectList(SaleFoodDO::getOrderId,order.getId(),SaleFoodDO::getStatus,RbtSaleFoodStatusEnum.SALE.getVal());
//        sendMealCode(list);
//        return list;
//    }
//
//    private void sendMealCode(List<SaleFoodDO> list) {
//        Map<String,List<SaleFoodDO>> stringListMap = list.stream().collect(Collectors.groupingBy(SaleFoodDO::getRobotSn));
//        for (Map.Entry<String, List<SaleFoodDO>> item : stringListMap.entrySet()) {
//            String sn = item.getKey();
//            List<SaleFoodDO> saleFoodDOList = item.getValue();
//            SaleFoodDO firstItem = saleFoodDOList.get(0);
//            MealCodeVO mealCodeVO = new MealCodeVO();
//            mealCodeVO.setCode(firstItem.getCode());
//            mealCodeVO.setSiteId(firstItem.getPoint());
//            mealCodeVO.setBoxNos(saleFoodDOList.stream().map(m-> m.getRobotBoxSn()).toList());
//            mqttMessageService.sendMealCode(sn,mealCodeVO);
//        }
//    }
//
//    private List<SaleFoodDO> getSalseFoodByToday(List<String> sns) {
//        LocalDate today = LocalDate.now();
//        LocalDateTime startTime = LocalDateTime.of(today, LocalTime.MIN);
//        LocalDateTime endTime = LocalDateTime.of(today, LocalTime.MAX);
//        LambdaQueryWrapper<SaleFoodDO> queryWrapper = new LambdaQueryWrapper<SaleFoodDO>();
//        queryWrapper.between(SaleFoodDO::getMealDate,startTime,endTime)
//                .eq(SaleFoodDO::getStatus, RbtSaleFoodStatusEnum.SALE.getVal());
//        if(sns != null && !sns.isEmpty()) {
//            queryWrapper.in(SaleFoodDO::getRobotSn,sns);
//        }
//        return saleFoodMapper.selectList(queryWrapper);
//    }
//
//}